home *** CD-ROM | disk | FTP | other *** search
- Path: tank.news.pipex.net!pipex!iol!usenet
- From: David Byrden <Goyra@iol.ie>
- Newsgroups: comp.lang.c++
- Subject: Re: stl newbie help
- Date: 28 Mar 1996 21:18:58 GMT
- Organization: Ireland On-Line
- Message-ID: <4jevo2$vae@nuacht.iol.ie>
- References: <315ae9a1.455438@news.en.com>
- NNTP-Posting-Host: dialup-193.dublin.iol.ie
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.22KIT (Windows; I; 16bit)
-
- dpsm@en.com (Dale P. Smith) wrote:
-
- >
- >What I want to understand how to do is declare a function class that
- >can be used with something like for_each:
- > for_each(Table.begin(), Table.end(), FunctionClassForEachThing);
- >
- >I know that I have to create a class that has operator() in it. What
- >should this thing look like? I can't find (or can't see) any examples
- >in the stl documentation.
- >
-
-
- Dale;
-
- The various algorithms require different functor signatures; void
- return, bool return, value return, one parameter, 2 parameters etc. In
- your example, this signature is required;
-
-
- void Function( String& ) ;
-
- So, we could use an ordinary function, like the one above;
-
- void AppendAnE( String& ) ;
-
- for_each(Table.begin(), Table.end(), AppendAnE );
-
-
- Or, we could do this;
-
- struct AppendA
- {
- char letter ;
- AppendA( const char& c ) : letter( c ) {}
- void operator()( String& ) ; // code it yourself
- } ;
-
-
- for_each(Table.begin(), Table.end(), AppendAn( 'e' ) );
-
-
- See?
-
- David
-
-
-